package com.storm.lib.task;

import java.util.List;

import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;


import com.storm.lib.base.BaseTask;
import com.storm.lib.template.ChatTempate;
import com.storm.lib.template.PushTemplate;
import com.storm.lib.util.BeanUtil;

/**
 * 
 * 类功能:同步聊天信息
 * @author johnny
 * @version 2011-8-31
 */
public class SyncChatTask extends BaseTask{

	
	@Override
	public void run() {
		ChatTempate chatTempate = (ChatTempate) BeanUtil.getBean("chatTempate");
		PushTemplate pushTemplate = (PushTemplate) BeanUtil.getBean("pushTemplate");
		for (IoSession ioSession : chatTempate.getChatCache().keySet()) {

			List<String> chatList = chatTempate.getChatCache().get(ioSession);
			if(chatList.size()>0){
				pushTemplate.pushChatToSession(chatList.toArray(new String[chatList.size()]), ioSession);
				chatList.clear();				
			}
		}
	}
	
	public SyncChatTask() {
		this.setRepeatDelayMS(3000);
		this.setFirstDelayMS(1000);
	}
	
}
